package com.lanou3g.zjl.chating.user.regist;

import android.app.ProgressDialog;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.easemob.EMError;
import com.easemob.chat.EMChatManager;
import com.easemob.exceptions.EaseMobException;
import com.lanou3g.zjl.chating.R;
import com.lanou3g.zjl.chating.user.login.LoginActivity;
import com.lanou3g.zjl.chating.base.BaseActivity;
import com.lanou3g.zjl.chating.utils.CheckEditText;

import butterknife.Bind;

/**
 * Created by dllo on 15/11/17.
 */
public class RegisterActivity extends BaseActivity {
    @Bind(R.id.username)
    EditText userNameEditText;
    @Bind(R.id.password)
    EditText passwordEditText;
    @Bind(R.id.confirm_password)
    EditText confirmPwdEditText;


//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_register);
//
//        userNameEditText = (EditText) findViewById(R.id.username);
//        passwordEditText = (EditText) findViewById(R.id.password);
//        confirmPwdEditText = (EditText) findViewById(R.id.confirm_password);
//    }

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initData() {

    }

    /**
     * 注册
     */
    public void register(View v) {
        final String username = userNameEditText.getText().toString().trim();
        final String password = passwordEditText.getText().toString().trim();
        String confirm_pwd = confirmPwdEditText.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "用户名不能为空!", Toast.LENGTH_SHORT).show();
            userNameEditText.requestFocus();
            return;
        } else if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "密码不能为空!", Toast.LENGTH_SHORT).show();
            passwordEditText.requestFocus();
            return;
        } else if (TextUtils.isEmpty(confirm_pwd)) {
            Toast.makeText(this, "确认密码不能为空!", Toast.LENGTH_SHORT).show();
            confirmPwdEditText.requestFocus();
            return;
        } else if (!password.equals(confirm_pwd)) {
            Toast.makeText(this, "两次输入的密码不一致,请重新输入!", Toast.LENGTH_SHORT).show();
            return;
        }


        if (CheckEditText.isMobile(username) || CheckEditText.isUserName(username)) {

        } else {
            Toast.makeText(this, "用户名格式不正确，请输入手机号或者4~8位只由字母, 数字, 和下划线组成的用户名", Toast.LENGTH_LONG).show();
            return;
        }

        if (CheckEditText.isPsw(password)) {

        } else {
            Toast.makeText(this, "密码格式不正确，请输入6~12位由字母和数字组成的密码", Toast.LENGTH_SHORT).show();
            return;
        }


        if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
            final ProgressDialog pd = new ProgressDialog(this);
            pd.setMessage("正在注册..");
            pd.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //调用sdk注册方法
                    try {
                        //在服务器中保存账号密码
                        EMChatManager.getInstance().createAccountOnServer(username, password);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (!RegisterActivity.this.isFinishing())
                                    pd.dismiss();
                                //保存用户名 保存在本地数据库中
//                                DempJelper.getInstance().setCurrentUserName(username);
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                                finish();
                            }
                        });
                    } catch (final EaseMobException e) {
                        runOnUiThread(new Runnable() {
                            public void run() {
                                if (!RegisterActivity.this.isFinishing())
                                    pd.dismiss();
                                int errorCode = e.getErrorCode();
                                if (errorCode == EMError.NONETWORK_ERROR) {
                                    Toast.makeText(getApplicationContext(), R.string.network_anomalies, Toast.LENGTH_SHORT).show();
                                } else if (errorCode == EMError.USER_ALREADY_EXISTS) {
                                    Toast.makeText(getApplicationContext(), R.string.User_already_exists, Toast.LENGTH_SHORT).show();
                                } else if (errorCode == EMError.UNAUTHORIZED) {
                                    Toast.makeText(getApplicationContext(), R.string.registration_failed_without_permission, Toast.LENGTH_SHORT).show();
                                } else if (errorCode == EMError.ILLEGAL_USER_NAME) {
                                    Toast.makeText(getApplicationContext(), R.string.illegal_user_name, Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(getApplicationContext(), R.string.Registration_failed, Toast.LENGTH_SHORT).show();
                                }
                            }
                        });
                    }
                }
            }).start();


        }

    }
}